package com.two.lbms.controller;

import com.two.lbms.base.BaseController;
import com.two.lbms.base.ResultInfo;
import com.two.lbms.model.User;
import com.two.lbms.service.UserService;
import com.two.lbms.utils.CookieUtil;
import com.two.lbms.vo.Admin;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/7/30 21:13
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @RequestMapping("/checkLogin")
    public ResultInfo checkLogin(User user, HttpServletResponse response){

        log.info("登录管理员信息 : {}",user);
        User newUser = userService.checkLogin(user);
        CookieUtil.setCookie("admin",newUser.getUsername(),response);
        return success(newUser);

    }
    @PutMapping("/updatePassword")
    public ResultInfo updatePassword(HttpServletRequest request,String oldPassword, String newPassword, String againPassword){

        String admin = CookieUtil.getCookieValue(request, "admin");
        userService.updatePassword(admin,oldPassword,newPassword,againPassword);
        return success();
    }


}
